using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Decav.Diagnostics;
using System.IO;
using Decav.Gatsb.Configuration;

namespace Decav.Gatsb.Diagnostics
{
    public static class GatsbDiagnostics
    {
        static GatsbDiagnostics()
        {
        }

        private static EventLog _log = new EventLog("Gatsb");

        public static void WriteException(string message, Exception ex)
        {
            if ( message == null )
                throw new ArgumentNullException( "message" );
        
            if ( ex == null )
                throw new ArgumentNullException( "ex" );
        

            string output = string.Format( "{0}\r\n\r\n{1}", message, DecavDiagnostics.GetExceptionDetails(ex));

            WriteLogEntry(output, EventLogEntryType.Error);
        }

        public static void WriteLogEntry(string message, EventLogEntryType messageType)
        {
            if (message == null)
                throw new ArgumentNullException("message");

            try
            {
                _log.WriteEntry(message, messageType);
            }
            catch { /* swallow exception */ }

            if (messageType == EventLogEntryType.Error)
            {
                try
                {
                    File.WriteAllText(GatsbConfigurationSection.Current.ErrorLogPath, message);
                }
                catch { /* swallow exception */ }
            }
        }
    }
}
